package com.lhf.springcloud.controller.student;

import com.lhf.springcloud.Entity.StudentCompleteData;
import com.lhf.springcloud.service.StudentCompleteDataService;
import com.lhf.springcloud.service.impl.StudentCompleteDataImpl;
import com.lhf.springcloud.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentCompleteData")
public class StudentCompleteDataController {
    @Autowired
    private StudentCompleteDataService studentCompleteData;
    /*
    * 根据身份证号查找学生数据
    * */
    @GetMapping("/findByIdCard")
    @ResponseBody
    public Map listUploadedFiles(String idCard){
        List<StudentCompleteData> allData = studentCompleteData.findstudentCompleteData(idCard);
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allData.size());
        map.put("data",allData);
        return map;
    }
    @GetMapping("/studentCompleteDataHtml")
    public String listUploadedFiles(){
        return "project/dataCenter/studentEssentialInformation(1)";
    }
    @GetMapping("/findByIdGrade")
    @ResponseBody
    public List<StudentCompleteData> findByIdGrade(){
        List<StudentCompleteData> byIdGrade = studentCompleteData.findByIdGrade();
        return byIdGrade;
    }
    /*
    * 分页查询所有数据查询
    * */
    @RequestMapping(value = "/findAllStudentCompleteData",method = RequestMethod.GET)
    @ResponseBody
    public Map findAllData(Integer page,Integer limit,String idCard,String studentName){
        if (page==null){
            page=1;
        }
        if (limit==null){
            limit=10;
        }
        page=(page-1)*limit;
        Map allStudentBasicData = studentCompleteData.findAllStudentBasicData(page,limit,idCard,studentName);
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allStudentBasicData.get("account"));
        map.put("data",allStudentBasicData.get("data"));
        return map;
    }
    @RequestMapping(value = "/findLogDataById",method = RequestMethod.GET)
    @ResponseBody
    public Map findLogDataById(Integer page,Integer limit,String id){
        if (page==null){
            page=1;
        }
        if (limit==null){
            limit=10;
        }
        page=(page-1)*limit;
        Map allStudentBasicData = studentCompleteData.findLogDataById(page,limit,id);
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allStudentBasicData.get("account"));
        map.put("data",allStudentBasicData.get("data"));
        return map;
    }
    /*
    * 插入测试数据接口
    * */
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    public void insert(){
        StudentCompleteData studentCompleteData = new StudentCompleteData();
        this.studentCompleteData.insert(studentCompleteData);
    }
    @RequestMapping(value = "/updateStudentCompleteData",method = RequestMethod.GET)
    public String updateData(String oldValue,String value,String id,String field){
        studentCompleteData.updateData(oldValue,value,id,field);
        return "redirect:/studentCompleteData/studentCompleteDataHtml";
    }
    //修改整条数据
    @RequestMapping(value = "/updateAllStudentCompleteData",method = RequestMethod.GET)
    public String updateAllStudentCompleteData(StudentCompleteData studentData,String id){
        studentCompleteData.updateAllStudentCompleteData(studentData,id);
        return "redirect:/studentCompleteData/studentCompleteDataHtml";
    }
    //文件上传
    @PostMapping("/upLoadStudentEssentialInformation")
    public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){
        if(file.isEmpty()) {
            return "redirect:/studentEssentialInformation";
        }
        List<StudentCompleteData> list1= new ArrayList<>();
        String s =null;
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getCourseListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 1; i < list.size(); i++) {
                List<Object> courseList = list.get(i);

                StudentCompleteData course = new StudentCompleteData();
                if (courseList.size()==0){
                    break;
                }
                course.setStudentName(courseList.get(0).toString());
                course.setStudentXJ(courseList.get(1).toString());
                course.setEntrance(courseList.get(2).toString());
                course.setStudentStatus(courseList.get(3).toString());
                course.setPolitical(courseList.get(4).toString());
                course.setFamilyPhotoNumber(courseList.get(5).toString());
                course.setStudentAddress(courseList.get(6).toString());
                course.setIdCard(courseList.get(7).toString());
                course.setPhotoNumber(courseList.get(8).toString());
                course.setStudentImgUrl(courseList.get(9).toString());
                course.setBiogenicLand(courseList.get(10).toString());
                course.setBiogenicLandType(courseList.get(11).toString());
                course.setStuId(courseList.get(12).toString());
                course.setEducation(courseList.get(13).toString());
                course.setSchLength(courseList.get(14).toString());
                course.setLevel(courseList.get(15).toString());
                course.setMajor(courseList.get(16).toString());
                course.setClasses(courseList.get(17).toString());
                course.setGraduationcertificatenum(courseList.get(18).toString());
                course.setNativeplace(courseList.get(19).toString());
                course.setSex(courseList.get(20).toString());
                course.setNation(courseList.get(21).toString());
                course.setAdmissionTime(courseList.get(22).toString());
                course.setGraduationTime(courseList.get(23).toString());
                course.setExamineeNum(courseList.get(24).toString());
                course.setNormalSchStu(courseList.get(25).toString());
                course.setDifficultStu(courseList.get(26).toString());
                course.setSchoolstatus(courseList.get(27).toString());
                course.setPhotoNumber(courseList.get(28).toString());
                course.setDormitoryInformation(courseList.get(29).toString());
                course.setDormitoryBuilding(courseList.get(30).toString());
                course.setDormitoryUnits(courseList.get(31).toString());
                course.setDormitoryFloor(courseList.get(32).toString());
                course.setDormitoryRoom(courseList.get(33).toString());
                list1.add(course);
                // 执行插入操作

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list1.size()>0){
            s = studentCompleteData.addStudentInfo(list1);
        }
        redirectAttributes.addFlashAttribute("messages", s);

        return "redirect:/studentCompleteData/studentCompleteDataHtml"; //重定向
    }
}
